/**
 * 
 */
package forum.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import forum.shared.Delimiters;
import forum.shared.ServerResponse;

/**
 * @author Mirsky
 *
 */
public class AdminScreen extends Screen {
	private VerticalPanel contentPanel;
	private FlexTable listsTbl;
	private ListBox promoteList;
	private ListBox demoteList;
	private Button closeBtn;
	private Label headerLbl;
	private Label errorLbl;
	private Button promoteBtn;
	private Button demoteBtn;
	
	public AdminScreen() {
		init();
	}

	private void populatePromoteList() {
		ForumServiceAsync forumSvc = GWT.create(ForumService.class);
		
		// Set up the callback object.
	    AsyncCallback<ServerResponse> callback = new AsyncCallback<ServerResponse>() {
	    	public void onFailure(Throwable caught) {
	    		errorLbl.setText("Communication error. Please retry later.");
	    		errorLbl.setVisible(true);
	    	}
	    	//On successful communication.
			@Override
			public void onSuccess(ServerResponse response) {
				if (response!=null && response.hasExecuted()) {
					for (String str : response.getResponse().split(Delimiters.MSG_PARTS)) {
						promoteList.addItem(str);
					}
	            } 
				else {
		    		errorLbl.setText(response!=null ? 
							response.getResponse() : "Unknown Error...");
		    		errorLbl.setVisible(true);

	            }
			}
	    };
	    forumSvc.sendMsg("PromoteList", callback);
	}

	private void populateDemoteList() {
		ForumServiceAsync forumSvc = GWT.create(ForumService.class);
		
		// Set up the callback object.
	    AsyncCallback<ServerResponse> callback = new AsyncCallback<ServerResponse>() {
	    	public void onFailure(Throwable caught) {
	    		errorLbl.setText("Communication error. Please retry later.");
	    		errorLbl.setVisible(true);
	    	}
	    	//On successful communication.
			@Override
			public void onSuccess(ServerResponse response) {
				if (response!=null && response.hasExecuted()) {
					for (String str : response.getResponse().split(Delimiters.MSG_PARTS))
						demoteList.addItem(str);
	            } 
				else {
		    		errorLbl.setText(response!=null ? 
							response.getResponse() : "Unknown Error...");
		    		errorLbl.setVisible(true);

	            }
			}
	    };
	    forumSvc.sendMsg("DemoteList", callback);	
	}

	/* (non-Javadoc)
	 * @see forum.client.Screen#createComponents()
	 */
	@Override
	protected void createComponents() {
		this.contentPanel = new VerticalPanel();
		this.promoteList = new ListBox();
		this.demoteList = new ListBox();
		this.listsTbl = new FlexTable();
		this.closeBtn = new Button("Close");
		this.headerLbl = new Label("Admin Panel");
		this.errorLbl = new Label();
		this.promoteBtn = new Button(">");
		this.demoteBtn = new Button("<");
	}

	/* (non-Javadoc)
	 * @see forum.client.Screen#getInput()
	 */
	@Override
	protected String getInput() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see forum.client.Screen#setActions()
	 */
	@Override
	protected void setActions() {
		this.closeBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				new MainScreen().setupDisplayInMainPanel();
			}
		});
		
		this.promoteBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				promote();
			}
		});
		
		this.demoteBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				demote();
			}
		});
	}

	protected void promote() {
		ForumServiceAsync forumSvc = GWT.create(ForumService.class);
		
		// Set up the callback object.
	    AsyncCallback<ServerResponse> callback = new AsyncCallback<ServerResponse>() {
	    	public void onFailure(Throwable caught) {
	    		errorLbl.setText("Communication error. Please retry later.");
	    		errorLbl.setVisible(true);
	    		promoteBtn.setEnabled(true);
	    	}
	    	//On successful communication.
			@Override
			public void onSuccess(ServerResponse response) {
				if (response!=null && response.hasExecuted()) {
					new AdminScreen().setupDisplayInMainPanel();
	            } 
				else {
		    		errorLbl.setText(response!=null ? 
							response.getResponse() : "Unknown Error...");
		    		errorLbl.setVisible(true);

	            }
				promoteBtn.setEnabled(true);
			}
	    };
	    
		// Make the call to the forum service.
	    String input = 
	    	this.promoteList.getItemText(promoteList.getSelectedIndex());
	    if (input != null && !input.isEmpty())
	    	forumSvc.sendMsg("Promote" + Delimiters.MSG_PARTS + input, callback);
	    else
	    	promoteBtn.setEnabled(true);
	}

	protected void demote() {
		ForumServiceAsync forumSvc = GWT.create(ForumService.class);
		
		// Set up the callback object.
	    AsyncCallback<ServerResponse> callback = new AsyncCallback<ServerResponse>() {
	    	public void onFailure(Throwable caught) {
	    		errorLbl.setText("Communication error. Please retry later.");
	    		errorLbl.setVisible(true);
	    		demoteBtn.setEnabled(true);
	    	}
	    	//On successful communication.
			@Override
			public void onSuccess(ServerResponse response) {
				if (response!=null && response.hasExecuted()) {
					new AdminScreen().setupDisplayInMainPanel();
	            } 
				else {
		    		errorLbl.setText(response!=null ? 
							response.getResponse() : "Unknown Error...");
		    		errorLbl.setVisible(true);

	            }
				demoteBtn.setEnabled(true);
			}
	    };
	    
		// Make the call to the forum service.
	    String input = 
	    	this.demoteList.getItemText(promoteList.getSelectedIndex());
	    if (input != null && !input.isEmpty())
	    	forumSvc.sendMsg("Demote" + Delimiters.MSG_PARTS + input, callback);
	    else
	    	demoteBtn.setEnabled(true);
	}

	/* (non-Javadoc)
	 * @see forum.client.Screen#setStyle()
	 */
	@Override
	protected void setStyle() {
		this.closeBtn.addStyleName("button");
		this.listsTbl.addStyleName("horizontalPanel");
		this.headerLbl.addStyleName("title");
		this.errorLbl.addStyleName("errorLabel");
		this.errorLbl.setVisible(false);
		this.promoteBtn.addStyleName("button");
		this.demoteBtn.addStyleName("button");
		this.contentPanel.addStyleName("horizontalPanel");
	}

	/* (non-Javadoc)
	 * @see forum.client.Screen#setTabOrder()
	 */
	@Override
	protected void setTabOrder() {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see forum.client.Screen#setupDisplayInMainPanel()
	 */
	@Override
	public void setupDisplayInMainPanel() {
		Panel mainPanel = MainPanel.get();
		mainPanel.clear();
		mainPanel.add(this.headerLbl);
		mainPanel.add(this.errorLbl);
		mainPanel.add(this.listsTbl);
		this.contentPanel.add(this.closeBtn);
		mainPanel.add(contentPanel);
	}

	/* (non-Javadoc)
	 * @see forum.client.Screen#setupScreenDisplay()
	 */
	@Override
	protected void setupScreenDisplay() {
		this.listsTbl.setText(0, 0, "Promote list");
		this.listsTbl.setText(0, 3, "Demote list");
		this.listsTbl.setWidget(1, 0, this.promoteList);
		this.listsTbl.setWidget(1, 1, promoteBtn);
		this.listsTbl.setWidget(1, 2, demoteBtn);
		this.listsTbl.setWidget(1, 3, this.demoteList);
		
		populateDemoteList();
		populatePromoteList();
		this.promoteList.setVisibleItemCount(10);
		this.demoteList.setVisibleItemCount(10);
	}

}
